<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="称重时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="车牌号码" prop="plantNo">
        <el-input
          v-model="queryParams.plantNo"
          placeholder="请输入车牌号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="场站名称" prop="stationName">
        <el-input
          v-model="queryParams.stationName"
          placeholder="请输入场站名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item  label="所属区域" prop="areaCode">
        <el-select v-model="queryParams.areaCode"  placeholder="请选择">
          <el-option
            v-for="item in townData"
            :key="item.areaCode"
            :label="item.areaName"
            :value="item.areaCode"
          ></el-option>
        </el-select>

      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="recordList" :max-height="tableHeight">
      <el-table-column label="场站名称" align="left" prop="stationName" width="300px" />
      <el-table-column label="车牌号码" align="center" prop="plantNo" />
      <el-table-column label="挂车号码" align="center" prop="plantNoGua" />
      <el-table-column label="称重时间" align="center" prop="weightTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.weightTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="称重总重(单位kg)" align="center" prop="grossWeight" />
      <el-table-column label="异常原因" align="center" prop="alarmReason" />
      <el-table-column label="所属区域" align="center" prop="areaCode" :formatter="converJurisdictionUnit"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import { nowaybillrecord } from "@/api/weight/abnormal";
  import { getAreaListByUserId } from "@/api/weight/station";


  export default {
    name: "nowaybillrecord",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 称重记录表格数据
        recordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        //称重弹窗
        openView: false,
        openVideoView: false,
        tableHeight: window.innerHeight - 300,
        //日期范围
        dateRange: [],
        // 是否超重
        dict_isOverWeight: [],

        townData: [],

        videoUrl: '',

        is_admin: false,

        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          stationCode: undefined,
          deviceCode: undefined,
          serialNumber: undefined,
          goodsCode: undefined,
          stationName: undefined,
          weightTime: undefined,
          operatorName: undefined,
          plantNo: undefined,
          plantColor: undefined,
          grossWeight: undefined,
          emptyWeight: undefined,
          netWeight: undefined,
          isOverWeight: undefined,
          plantNoGua: undefined,
          zPlatePhoto: undefined,
          rearSidePhoto: undefined,
          areaCode: undefined,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          stationCode: [
            { required: true, message: "场站编号不能为空", trigger: "blur" }
          ],
          deviceCode: [
            { required: true, message: "称重设备编号不能为空", trigger: "blur" }
          ],
          serialNumber: [
            { required: true, message: "称重业务内部流水号不能为空", trigger: "blur" }
          ],
          stationName: [
            { required: true, message: "场站名称不能为空", trigger: "blur" }
          ],
          weightTime: [
            { required: true, message: "称重时间不能为空", trigger: "blur" }
          ],
          operatorName: [
            { required: true, message: "操作人姓名不能为空", trigger: "blur" }
          ],
          plantNo: [
            { required: true, message: "车牌号码不能为空", trigger: "blur" }
          ],
          plantColor: [
            { required: true, message: "车牌颜色不能为空", trigger: "blur" }
          ],
          grossWeight: [
            { required: true, message: "总重(单位kg)不能为空", trigger: "blur" }
          ],
          emptyWeight: [
            { required: true, message: "空重(单位kg)不能为空", trigger: "blur" }
          ],
          netWeight: [
            { required: true, message: "净重(单位kg)不能为空", trigger: "blur" }
          ],
        },
        frUrl: '',
        openIframe: false,
        openVideoPlayer: false,
        myPlayer: undefined
      };
    },
    created() {
      let date = new Date();
      let today= this.timeFormat(date) //当前日期
      // 默认日期赋值
      this.dateRange = [today, today];

      const stationCode = this.$route.params && this.$route.params.stationCode;
      this.queryParams.stationCode=stationCode;
      this.getList();
      this.getDicts("is_over_weight").then(response => {
        this.dict_isOverWeight = response.data;
      });

      const roles = this.$store.getters && this.$store.getters.roles;
      for (let rol of roles){
        if ('admin'==rol){
          this.is_admin=true;
        }
      }

      getAreaListByUserId().then(response =>{
        this.townData=response.data;
      });
    },
    mounted() {

    },
    methods: {
      /** 查询称重记录列表 */
      getList() {
        this.loading = true;
        nowaybillrecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.recordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      info_close() {
        this.openView =false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          stationCode: undefined,
          deviceCode: undefined,
          serialNumber: undefined,
          goodsCode: undefined,
          stationName: undefined,
          weightTime: undefined,
          operatorName: undefined,
          plantNo: undefined,
          plantColor: undefined,
          grossWeight: undefined,
          emptyWeight: undefined,
          netWeight: undefined,
          isOverWeight: undefined,
          plantNoGua: undefined,
          zPlatePhoto: undefined,
          rearSidePhoto: undefined,
          delFlag: undefined,
          createTime: undefined,
          updateTime: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        let date = new Date();
        let today= this.timeFormat(date) //当前日期
        // 默认日期赋值
        this.dateRange = [today, today];
        this.handleQuery();
      },
      //详细信息
      handleView(row) {
        this.reset();
        const id = row.id
        this.$router.push({
          path:"/weigh/recorddetail",
          query:{
            id:row.id
          }
        })
      },
      timeFormat: function (date) {
        if (!date || typeof(date) === "string") {
          this.error("参数异常，请检查...");
        }
        var y = date.getFullYear(); //年
        var m = date.getMonth() + 1; //月
        var d = date.getDate(); //日

        return y + "-" + (m < 10 ? '0' + m : m) + "-" + (d < 10 ? '0' + d : d);
      },
      // 所属区域转换
      converJurisdictionUnit(row,column){
        let areaName = '';
        this.townData.forEach(function (_val,index) {
          if(row.areaCode==_val.areaCode){
            areaName = _val.areaName;
            return true;
          }
        });
        return areaName;
      },
    }
  };
</script>
